<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<link rel="stylesheet"
	href="css/shoppingCart.css?id=<%=new Date().getTime()%>" />
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
	/* function www(){
		$.post("shoppingcarts.shtml",function(data){
			$.each(data,function(){
				var shopcarts = "<div class='cart-style-two clear' style='height: 75px; line-height: 75px;'><div><input type='checkbox' name='checked-dan' class='checked-dan' style='margin-top: 30px;margin-left:15px;' /></div><div style='margin-left: 40px;'><img src='"+this.cartimg+"' width='50px' style='margin-top: 10px;' /></div><div style='margin-left: 8px;'><p>"+this.cartname+"</p></div><div style='margin-left: 125px;'><span>￥</span><span>"+this.cartprice+"</span></div><div style='margin-left: 70px;margin-top:20px;'><button style='padding: 6px; width: 30px; box-sizing: border-box; text-align: center;' class='jia' type='button'>+</button><input type='text' name='num' class='num' value='"+this.cartnum+"' style='height: 25px; width: 45px; text-align: center; border-left: 0px; border-right: 0px; border-top: 1px solid #979797; border-bottom: 1px solid #979797' /><button style='padding: 6px; width: 30px; box-sizing: border-box; text-align: center;' class='jian' type='button'>-</button></div><div style='margin-left: 60px;'><span>￥</span><span class='smallji'>"+this.carttotal+"</span></div><div style='margin-left: 115px;'>删除</div></div>";
				$(".scart-style-one").append(shopcarts);
			})
		})
	} */
	
	$(function() {
		var pp = false;
		
		var l = 0;
		var zongprice = parseFloat($("#totprice").html());
		$(".jia").click(function() {
			var jiaindex = $(".jia").index(this);
			
			var dianjia = $(".danjia:eq("+jiaindex+")").html();
			var num = parseInt($(".num:eq(" + jiaindex + ")").val());
			if (num >= 50) {
				
				$(".num:eq(" + jiaindex + ")").val("50");
				$(".smallji:eq("+jiaindex+")").html(((parseInt(num))*parseFloat(dianjia)).toFixed(2));
				return;
			}
			if (num < 50) {

				$(".num:eq(" + jiaindex + ")").val(num + 1);

			} else {
				alert("库存不足")
			}
			$(".smallji:eq("+jiaindex+")").html(((parseInt(num)+1)*parseFloat(dianjia)).toFixed(2));
			var totprice = 0;
			$(".checked-dan:checked").each(function(){
				var row = $(this).parent().parent();
				var price = parseFloat(row.find("span.smallji").html());
				totprice += price;
			});
				$("#totprice").html(totprice.toFixed(2));
		})
		//
		$(".jian").click(function() {
			var jianindex = $(".jian").index(this);
			var dianjia = $(".danjia:eq("+jianindex+")").html();
			var num = parseInt($(".num:eq(" + jianindex + ")").val());
			if (num == 0) {
				$(".num:eq(" + jianindex + ")").val("1");
				return;
			}
			if (num > 1) {
				$(".num:eq(" + jianindex + ")").val(num - 1);
			} else {
				alert("该商品最少买一件")
			}
			if(num==1){
				$(".smallji:eq("+jianindex+")").html(((parseInt(num))*parseFloat(dianjia)).toFixed(2));
				return;
			}
			$(".smallji:eq("+jianindex+")").html(((parseInt(num)-1)*parseFloat(dianjia)).toFixed(2));
			var totprice = 0;
			$(".checked-dan:checked").each(function(){
				var row = $(this).parent().parent();
				var price = parseFloat(row.find("span.smallji").html());
				totprice += price;
			});
				$("#totprice").html(totprice.toFixed(2));
		})
		
		var pd1 = "false";
		$("#batch").click(function(){
			if($(":checkbox[name='cartid']:checked").length==0){
				alert("请选中你要删除的商品");
				return;
			}
			$("#form2").attr("action","shoppingCart.shtml");
			
			$("#form2").trigger("submit");
		})
		
		$("#checked-all").click(function(){
			var pd = "false";
			var $isChecked = $(this).is(":checked");
			if ($isChecked) {
				pd = "true";
				$(":checkbox[name='cartid'][class='checked-dan']").attr(
						"checked", "checked");
				var totprice = 0;
				var s=$(":checkbox[name='cartid']:checked").length
				if($(":checkbox[name='cartid']:checked").length>0){
					$("#nextdan").css("background-color","#e1c390");
				}
				
				$(".checked-dan:checked").each(function(){
					var row = $(this).parent().parent();
					var price = parseFloat(row.find("span.smallji").html());
					totprice += price;
				});
					$("#totprice").html(totprice.toFixed(2));
				
			} else {
				pd = "false";
				$(":checkbox[name='cartid'][class='checked-dan']").removeAttr(
						"checked");
				$("#nextdan").css("background-color","#ccc");
				$("#totprice").html("0.00");
			}
		})
		
		
		$(".num").keyup(function(){
		  var numindex = $(".num").index(this);
		  var dianjia = $(".danjia:eq("+numindex+")").html();
			var s = $(this).val();
			if(!isNaN(s)){
			if(s>50){
				alert("当前库存不足");
				$(this).val(50);
				$(".smallji:eq("+numindex+")").html(((parseInt(50))*parseFloat(dianjia)).toFixed(2));
				return;
			}
			if(s==""){
				$(this).val(1);
				return;
			}
			$(".smallji:eq("+numindex+")").html(((parseInt(s))*parseFloat(dianjia)).toFixed(2));
			}else{
				alert("请输入正确的值")
				$(this).val(1);
			}
					
		})
		
		
		
		$(".checked-dan").click(function(){
			
			//获取所有选中的复选框
			var totprice = 0;
			
			
			/* var index = $(".checked-dan").index(this); */
			
			
		var s=$(":checkbox[name='cartid']:checked").length
		//alert(s);
		if($(":checkbox[name='cartid']:checked").length>0){
			$(":checkbox[name='checked-all'][id='checked-all']").removeAttr(
			"checked");
			$("#nextdan").css("background-color","#e1c390");
		}else{
			$("#nextdan").css("background-color","#ccc");
		}
		
		$(".checked-dan:checked").each(function(){
			var row = $(this).parent().parent();
			var price = parseFloat(row.find("span.smallji").html());
			totprice += price;
		});
			$("#totprice").html(totprice.toFixed(2));
		}) 
		
		
		
		
		
		$("#nextdan").click(function(){
			var s = "${user.namesp}";
				 if(s!=""){
					 if($(":checkbox[name='cartid']:checked").length>0){
						 $("#form2").attr("action","goPayMent.shtml");
							$("#form2").trigger("submit");
						 return;
					 }
							$(":checkbox[name='cartid'][class='checked-dan']").attr(
									"checked", "checked");
							var totprice = 0;
							 var s=$(":checkbox[name='cartid']:checked").length
							if($(":checkbox[name='cartid']:checked").length>0){
								$("#nextdan").css("background-color","#e1c390");
							}
							for (var i = 0; i < s; i++) {
								totprice = parseFloat(totprice)+parseFloat($(".smallji:eq("+i+")").html());
							}
								$("#totprice").html(totprice.toFixed(2));
				}else{
					var s = confirm("是否登录？")
					if(s){
						$("#goumai").fadeIn(300);
						setTimeout(function(){
							$("#goumai003").show(400);
						}, 250)
					}
					
				} 
			
		
		})
		
		var s = "${user.namesp}";
		 if(s!=""){
					$(":checkbox[name='cartid'][class='checked-dan']").attr(
							"checked", "checked");
					var totprice = 0;
					var s=$(":checkbox[name='cartid']:checked").length
					if($(":checkbox[name='cartid']:checked").length>0){
						$("#nextdan").css("background-color","#e1c390");
					}
					for (var i = 0; i < s; i++) {
						
						totprice = parseFloat(totprice)+parseFloat($(".smallji:eq("+i+")").html());
						
					}
						$("#totprice").html(totprice.toFixed(2));
		}
		
	})
</script>
<body>
	<%@ include file="Header.jsp"%>
	<form action="shoppingCart.shtml" id="form2">
	<div class="cart-style-width">
		<div class="cart-style-width-ern" style="margin-top: 10px;">
			<div
				style="height: 35px; line-height: 0px; background-color: #f5f5f5; padding: 0px 20px 0px 20px; box-sizing: border-box; border: 1px solid #ddd; margin-bottom: 20px;">
				<span> <input type="checkbox" name="checked-all"
					id="checked-all" style="margin-top: 12px;" />
				</span> <span> 全选 </span> <span style="margin-left: 90px;"> 商品信息 </span> <span
					style="margin-left: 210px;"> 单价 </span> <span
					style="margin-left: 120px;"> 数量 </span> <span
					style="margin-left: 120px;"> 小计 </span> <span
					style="margin-left: 120px;"> 操作 </span>
			</div>
			<div class="scart-style-one clear"
				style="border: 1px solid #ddd; padding: 15px; box-sizing: border-box; margin-bottom: 25px;">
				<c:forEach items="${shoppingCards }" var="shoppingCard">
					<div class="cart-style-two clear"
						style="height: 75px; line-height: 75px;">
						<div>
							<input type="checkbox" name="cartid" value="${shoppingCard.cartid }" class="checked-dan"
								style="margin-top: 30px; margin-left: 15px;" />
						</div>
						<div style="margin-left: 40px;">
							<img src="${shoppingCard.cartimg }" width="50px"
								style="margin-top: 10px;" />
						</div>
						<div style="margin-left: 8px;">
							<p>${shoppingCard.cartname }${shoppingCard.cartid }</p>
						</div>
						<div style="margin-left: 125px;">
							<span>￥</span><span class="danjia">${shoppingCard.cartprice }</span>
						</div>
						<div style="margin-left: 70px;">
							<button
								style="padding: 6px; width: 30px; box-sizing: border-box; text-align: center;"
								class="jia" type="button">+</button>
							<input type="text" name="num" class="num"
								value="${shoppingCard.cartnum }"
								style="height: 25px; width: 45px; text-align: center; border-left: 0px; border-right: 0px; border-top: 1px solid #979797; border-bottom: 1px solid #979797" />
							<button
								style="padding: 6px; width: 30px; box-sizing: border-box; text-align: center;"
								class="jian" type="button">-</button>
						</div>
						<div style="margin-left: 60px;">
							<span>￥</span><span class="smallji">${shoppingCard.carttotal }</span>
						</div>
						<div style="margin-left: 115px;">删除</div>
					</div>
				</c:forEach>
			</div>
			<div
				style="height: 80px; line-height: 80px; background-color: #f4f4f4; border: 1px solid #ddd;padding:0px 10px 0px 20px;box-sizing:border-box;"
				class="clear">
				<div class="left" id="batch">批量删除</div>
				<div class="right" style="margin-right:20px;">
					<p class="left" style="margin-right:30px;">
						<span>应付总金额：</span> <span>￥</span> <span id="totprice">0.00</span>
					</p>
					<p style="width:120px;background-color:#ccc;text-align:center;color:whtie;font-size:16px;" class="left" id="nextdan">
					下单
					</p>
				</div>
			</div>
			<div></div>
		</div>
	</div>
	</form>
</body>
</html>